import xadmin
from .models import Goods, GoodsCategory, GoodsImage, GoodsCategoryBrand, Banner, HotSearchWords, IndexAd


class GoodsCategoryAdmin(object):
    list_display = ['name', 'category_type', 'parent_category', 'add_time']
    list_filter = ['name', 'category_type', 'parent_category']
    search_fields = ['name']


class GoodsBrandAdmin(object):
    list_display = ['category', 'image', 'name', 'desc']

    def get_context(self):
        context = super(GoodsBrandAdmin, self).get_context()
        if 'form' in context:
            context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1)
        return context


class GoodsAdmin(object):
    list_display = ['name', 'click_num', 'sold_num', 'fav_num', 'goods_num', 'market_price', 'shop_price',
                    'goods_brief', 'goods_desc', 'is_new', 'is_hot', 'add_time']
    search_fields = ['name']
    list_editable = ['is_hot']
    list_filter = ['name', 'click_num', 'sold_num', 'fav_num', 'goods_num', 'market_price', 'shop_price',
                   'goods_brief', 'goods_desc', 'is_new', 'is_hot', 'add_time', 'category__name']
    style_fields = {'goods_desc': 'ueditor'}

    class GoodsImagesInline(object):
        model = GoodsImage
        exclude = ['add_time']
        extra = 1
        style = 'tab'

    inlines = [GoodsImagesInline]


class BannerAdmin(object):
    list_display = ['goods', 'image', 'index']


class HotSearchAdmin(object):
    list_display = ['keywords', 'index', 'add_time']


class IndexAdAdmin(object):
    list_display = ['category', 'goods']


xadmin.site.register(GoodsCategory, GoodsCategoryAdmin)
xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin)
xadmin.site.register(Goods, GoodsAdmin)

xadmin.site.register(Banner, BannerAdmin)
xadmin.site.register(HotSearchWords, HotSearchAdmin)
xadmin.site.register(IndexAd, IndexAdAdmin)
